package com.tang.tangaiagent.demo.invoke;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 阿里云灵积HTTP模型调用
 */

public class HttpAIInvoke {
    public static void main(String[] args) {
        // 替换为你的 API Key
        String apiKey = TestApiKey.API_KEY;

        // 请求 URL
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";

        // 构建请求体 JSON
        JSONObject requestBody = JSONUtil.createObj()
                .put("model", "qwen-plus")
                .put("input", JSONUtil.createObj()
                        .put("messages", JSONUtil.createArray()
                                .put(JSONUtil.createObj()
                                        .put("role", "system")
                                        .put("content", "You are a helpful assistant.")
                                )
                                .put(JSONUtil.createObj()
                                        .put("role", "user")
                                        .put("content", "你是谁？")
                                )
                        )
                )
                .put("parameters", JSONUtil.createObj()
                        .put("result_format", "message")
                );

        // 发送 POST 请求
        HttpResponse response = HttpRequest.post(url)
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .body(requestBody.toString())
                .execute();

        // 输出响应结果
        System.out.println("Status Code: " + response.getStatus());
        System.out.println("Response Body: " + response.body());
    }
}